<?php

class HistJurnalController extends Controller
{
	/**
	 * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
	 * using two-column layout. See 'protected/views/layouts/column2.php'.
	 */
	public $layout='//layouts/column2';

	/**
	 * @return array action filters
	 */
	
	/**
	 * Displays a particular model.
	 * @param integer $id the ID of the model to be displayed
	 */
	public function actionView($id)
	{
		$this->render('view',array(
			'model'=>$this->loadModel($id),
		));
	}
	
	public function actionCetak($nomor){
		$sql = " SELECT c.code, h.description, h.debet, h.credit FROM hist_jurnal h left join mst_coa c on h.mst_account_id=c.id WHERE jurnal_no = '".$nomor."' ";
		$data  = Yii::app()->db->createCommand($sql)->queryAll();
		$model = HistJurnal::model()->find("jurnal_no='".$nomor."'");
	$session=new CHttpSession;
            $session->open();
		Yii::import('application.extensions.giiplus.bootstrap.*');
		require_once('tcpdf/tcpdf.php');
		require_once('tcpdf/config/lang/eng.php');
              
		
		$html = $this->renderPartial('cetak', array(
				'data'=>$data,
				'model'=>$model,
			), true);
		
		//die($html);
		
		$pdf = new TCPDF();
			$pdf->SetCreator(PDF_CREATOR);
			$pdf->SetAutoPageBreak(TRUE, 0);
			$pdf->setPrintHeader(false);
			$pdf->setPrintFooter(false);
			$pdf->SetAutoPageBreak(TRUE, 0);
			$pdf->SetFont('dejavusans', '', 7);
			$pdf->AddPage();
			$pdf->writeHTML($html, true, true, true, true, '');
			$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
			$pdf->LastPage();
		$pdf->Output("report_bank.pdf", "I");
	}

	/**
	 * Creates a new model.
	 * If creation is successful, the browser will be redirected to the 'view' page.
	 */
	 
	public function actionEdit()
	{
		$model=new HistJurnal;
		$sql = " SELECT * FROM hist_jurnal  WHERE jurnal_no = '".$_REQUEST['nomor']."' ";
		$data  = Yii::app()->db->createCommand($sql)->queryAll();
		
		if (isset($_POST['HistJurnal'])) {
		
		}
		
		$this->render('edit',array(
			'data'=>$data,
			'model'=>$model
		));
	}
	public function actionCreate()
	{
		$model=new HistJurnal;
		

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if (isset($_POST['HistJurnal'])) {
			$account_code = $_POST['account_code'];
			$desc = $_POST['desc'];
			$debet = $_POST['debet'];
			$credit = $_POST['credit'];
			$nomor = "";
			
			for($i=0;$i<10;$i++)
			{
				if($account_code[$i]!=""){
					$model =new HistJurnal;
					//$model->jurnal_no = $_POST['HistJurnal']['jurnal_no'];
					if($i != 0){
						$model->jurnal_no = $nomor;
					}
					$model->trx_date = date('Y-m-d',strtotime($_POST['HistJurnal']['trx_date']));
					$model->debet = $debet[$i];
					$model->credit = $credit[$i];
					$model->description = $desc[$i];
					$model->mst_account_id = $account_code[$i];
					$model->save();
					if($i==0){
							$nomor = "JN-".date("Ymd").$model->id;
							HistJurnal::model()->updateByPk($model->id,array('jurnal_no'=>$nomor));
						}
				}
			}
			$this->redirect(array('admin'));
		}
		

		$this->render('create',array(
			'model'=>$model,
		));
	}

	/**
	 * Updates a particular model.
	 * If update is successful, the browser will be redirected to the 'view' page.
	 * @param integer $id the ID of the model to be updated
	 */
	public function actionUpdate($id)
	{
		$model=$this->loadModel($id);

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if (isset($_POST['HistJurnal'])) {
			$model->attributes=$_POST['HistJurnal'];
			if ($model->save()) {
				$this->redirect(array('view','id'=>$model->id));
			}
		}

		$this->render('update',array(
			'model'=>$model,
		));
	}

	/**
	 * Deletes a particular model.
	 * If deletion is successful, the browser will be redirected to the 'admin' page.
	 * @param integer $id the ID of the model to be deleted
	 */
	public function actionDelete($id)
	{
		if (Yii::app()->request->isPostRequest) {
			// we only allow deletion via POST request
			$this->loadModel($id)->delete();

			// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
			if (!isset($_GET['ajax'])) {
				$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
			}
		} else {
			throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
		}
	}

	/**
	 * Lists all models.
	 */
	public function actionIndex()
	{
		$dataProvider=new CActiveDataProvider('HistJurnal');
		$this->render('index',array(
			'dataProvider'=>$dataProvider,
		));
	}

	/**
	 * Manages all models.
	 */
	public function actionAdmin()
	{
		$model=new HistJurnal('search');
		$model->unsetAttributes();  // clear any default values
		if (isset($_GET['HistJurnal'])) {
			$model->attributes=$_GET['HistJurnal'];
		}

		$this->render('admin',array(
			'model'=>$model,
		));
	}

	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer $id the ID of the model to be loaded
	 * @return HistJurnal the loaded model
	 * @throws CHttpException
	 */
	public function loadModel($id)
	{
		$model=HistJurnal::model()->findByPk($id);
		if ($model===null) {
			throw new CHttpException(404,'The requested page does not exist.');
		}
		return $model;
	}

	/**
	 * Performs the AJAX validation.
	 * @param HistJurnal $model the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if (isset($_POST['ajax']) && $_POST['ajax']==='hist-jurnal-form') {
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
}